<template>
	<view class="container">
		<view>
			<uni-popup ref="popup" type="top" background-color="#fff">
				<view class="popup-content">
					<!-- <uni-forms ref="queryForm" :modelValue="queryParams">
						<uni-forms-item label="案件编号" label-width="85">
							<uni-easyinput v-model="queryParams.caseCode" placeholder="案件编号"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item label="上报时间" label-width="85">
							<uni-datetime-picker v-model="queryParams.reportTime" type="daterange"/>
						</uni-forms-item>
					</uni-forms>
					<view style="display: flex;">
						<button size="mini" class="common-btn other-btn" @click="handleRepeat">重置</button>
						<button size="mini" class="common-btn primary-btn" @click="handleQuery">查询</button>
					</view> -->
					<case-query-form @query="handleQuery"></case-query-form>
				</view>
			</uni-popup>
		</view>
		<view class="uni-list">
			<case-list-content :list-data="listData" :rows="rows">
				<template #btns="item">
					<button class="common-btn other-btn nav-btn" size="mini" @click="handleNav(item.data)">导航</button>
					<button class="common-btn primary-btn nav-btn"  size="mini" @click="handleDeal(item.data)">办理</button>
					<button class="common-btn" :plain="item.data.status == '1'" type="warn"  size="mini" @click="handleJYJ(item.data)">{{item.data.status == '1'?'取消急要件':'急要件'}}</button>
				</template>
			</case-list-content>
		</view>
		<uni-load-more :status="status" :icon-size="16" :content-text="contentText" />
		<uni-popup ref="mapPopup" type="bottom" background-color="#fff">
			<view class="map-popup-content">
				<view>
					<image src=""></image>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { getDicts } from "@/api/system/dict/data";
	import { dateUtils } from  '@/utils/util.js';
	import { getWaitCaseList } from '@/api/dums/caw/caseInfo.js'
	import { GPS }  from '@/utils/GPS.js';
	import {addCawCaseExigence} from "@/api/dums/caw/cawCaseExigence";
	export default {
		data(){
			return {
				queryParams:{
					caseCode:"",
					remark4:"供水类",
					reportTime:[]
				},
				page:1,
				size:10,
				total:0,
				listData: [],
				rows:[{
					labelName:"案件编号",
					valueName:"caseCode"
				},{
					labelName:"当前环节",
					valueName:"linkName"
				},{
					labelName:"问题来源",
					valueName:"caseSource"
				},{
					labelName:"案件类型",
					valueName:"caseTypeName"
				},{
					labelName:"问题描述",
					valueName:"caseDesc"
				}],
				reload: false,
				status: 'more',
				adpid: '',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				sourceOptions:[],
				// 目标纬度
				latitude: '',
				// 目标经度
				longitude: '',
				// 目标地址名称
				name: '',
				// 我自己的位置经纬度（百度地图需要传入自己的经纬度进行导航）
				selfLocation: {
					latitude: '',
					longitude: ''
				}
			}
		},
		methods:{
			getList() {
				uni.showLoading({
					title:"加载中"
				})
				if (this.total && !this.reload) {
					//说明已有数据，目前处于上拉加载
					this.status = 'loading';
				}
				if(this.page == 1){
					this.reload = true;
					this.listData = [];
				}
				if(this.reload){
					uni.stopPullDownRefresh();
				}
				this.queryParams.pageSize = this.size;
				this.queryParams.pageNum = this.page;
				getWaitCaseList(this.queryParams).then(res => {
					uni.hideLoading();
					if(res.code == 200){
						//console.log(JSON.stringify(res));
						let list = res.rows;
						this.total = res.total;
						list.forEach(item => {
							if(item.rptChannelId){
								this.sourceOptions.forEach(opt => {
									if(opt.dictValue == item.rptChannelId){
										item.caseSource = opt.dictLabel;
									}
								})
							}
						})
						this.listData = this.reload ? list : this.listData.concat(list);
						this.reload = false;
						//判断是否最后一页
						if(this.size*this.page>=res.total){
							this.status="noMore"
						}
					}
				})
			},
			handleQuery(params){
				/* if(this.queryParams.reportTime &&　this.queryParams.reportTime.length>0){
					let params = {
						"rptTimeStart":this.queryParams.reportTime[0]+" 00:00:00",
						"rptTimeEnd":this.queryParams.reportTime[1]+" 23:59:59"
					}
					this.queryParams.params = params;
				} */
				this.queryParams = params;
				this.page = 1;
				this.getList();
				this.$refs.popup.close();
			},
			handleRepeat(){
				this.queryParams = {};
			},
			handleDeal(item){
				console.log(JSON.stringify(item));
				// console.log(item.caseId);
				// uni.navigateTo({
				// 	url:'../case-deal/index?caseId='+item.caseId+'&linkForm='+item.linkForm
				// })
				uni.navigateTo({
					url:'/pages/dums/case-deal/index?selectedCase='+JSON.stringify(item)
				})
			},
			handleNav(item){
				let position = GPS.gcj_encrypt(Number(item.posY),Number(item.posX))
				console.log(JSON.stringify(position));
				this.latitude = Number(item.posY);
				this.longitude = Number(item.posX);
				this.name = item.casePosDesc;
				this.handleNavigation();
				/* uni.openLocation({
					latitude: position.lat,
					longitude: position.lon,
					success: function () {
						console.log('success');
					}
				}); */
			},
			handleJYJ(item){
				let flag = "急要件";
				if(item.status && item.status == '1'){
					flag = "取消急要件"
				};
				let _this = this;
				uni.showModal({
					title:flag,
					editable:true,
					placeholderText:"说明",
					success(e) {
						
						if(e.confirm){
							if(e.content){
								let caseExigenceForm = {
									caseId:item.caseId,
									status:item.status=='1'?"0":"1",
									settingDesc:e.content
								}
								addCawCaseExigence(caseExigenceForm).then(res=>{
									console.log(JSON.stringify(res));
									if(res.code == 200){
										_this.$modal.msgSuccess(res.msg);
										_this.page = 1;
										_this.getList();
									}else{
										_this.$modal.msgError(res.msg);
									}
								});
							}else{
								_this.$modal.showToast("请输入说明");
							}
						}
					}
				})
			},
			handleNavigation() {
				const _this = this
				if (!this.latitude || !this.longitude || !this.name) {
					this.$modal.showToast("案件定位无效，无法导航");
					return
				}
				// 判断系统安装的地图应用有哪些, 并生成菜单按钮
				let _mapName = [
					{title: '高德地图', name: 'amap', androidName: 'com.autonavi.minimap', iosName: 'iosamap://'},
					{title: '百度地图', name: 'baidumap', androidName: 'com.baidu.BaiduMap', iosName: 'baidumap://'},
					{title: '腾讯地图', name: 'qqmap', androidName: 'com.tencent.map', iosName: 'qqmap://'},
				]
				// 根据真机有的地图软件 生成的 操作菜单
				let buttons = []
				let platform = uni.getSystemInfoSync().platform
				platform === 'android' && _mapName.forEach(item => {
					if (plus.runtime.isApplicationExist({pname: item.androidName})) {
						buttons.push(item)
					}
				})
				platform === 'ios' && _mapName.forEach(item => {
					console.log(item.iosName)
					if (plus.runtime.isApplicationExist({action: item.iosName})) {
						buttons.push(item)
					}
				})
				if (buttons.length) {
					plus.nativeUI.actionSheet({ //选择菜单
					title: "选择地图应用",
					cancel: "取消",
					buttons: buttons
					}, function (e) {
						let _map = buttons[e.index - 1]
						_this.openURL(_map, platform)
					})
				} else {
					uni.showToast({
						title: '请安装地图软件',
						icon: 'none'
					})
					return
				}
			},
	
			// 打开第三方程序实际应用
			openURL(map, platform) {
				let _defaultUrl = {
					android: {
						"amap": `amapuri://route/plan/?sid=&did=&dlat=${this.latitude}&dlon=${this.longitude}&dname=${this.name}&dev=0&t=0`,
						'qqmap': `qqmap://map/routeplan?type=drive&to=${this.name}&tocoord=${this.latitude},${this.longitude}&referer=fuxishan_uni_client`,
						'baidumap': `baidumap://map/direction?origin=${this.selfLocation.latitude},${this.selfLocation.longitude}&destination=name:${this.name}|latlng:${this.latitude},${this.longitude}&coord_type=wgs84&mode=driving&src=andr.baidu.openAPIdemo"`
					},
					ios: {
						"amap": `iosamap://path?sourceApplication=fuxishan_uni_client&dlat=${this.latitude}&dlon=${this.longitude}&dname=${this.name}&dev=0&t=0`,
						'qqmap': `qqmap://map/routeplan?type=drive&to=${this.name}&tocoord=${this.latitude},${this.longitude}&referer=fuxishan_uni_client`,
						'baidumap': `baidumap://map/direction?origin=${this.selfLocation.latitude},${this.selfLocation.longitude}&destination=name:${this.name}|latlng:${this.latitude},${this.longitude}&mode=driving&src=ios.baidu.openAPIdemo`
					}
				}
				let newurl = encodeURI(_defaultUrl[platform][map.name]);
				console.log(newurl)
				plus.runtime.openURL( newurl, function(res){
					console.log(res)
					uni.showModal({
					content: res.message
					})
				}, map.androidName ? map.androidName : '');
			}
		},
		
		onLoad() {
			//问题来源
			getDicts("case_source").then(response => {
				this.sourceOptions = response.data;
			});
			let _this = this;
			uni.getLocation({
				type: 'wgs84',
				success: function (res) {
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.latitude);
					_this.selfLocation.latitude = res.latitude;
					_this.selfLocation.longitude = res.longitude;
				}
			});
			
			this.page = 1;
		},
		onShow() {
			this.getList();
		},
		onNavigationBarButtonTap : function (e) {
			this.$refs.popup.open()
		},
		onPullDownRefresh(){
			this.page = 1;
			this.getList();
		},
		onReachBottom(){
			this.page++;
			if(this.listData.length < this.total){
				this.status = 'more';
			}else{
				this.status = 'noMore';
				return;
			}
			this.getList();
		}
	}
</script>

<style lang="scss">
	.popup-content {
		padding: 15px;
		height: 500px;
		background-color: #fff;
	}
	.nav-btn{
		margin-right: 15px;
	}
</style>